package cn.iocoder.yudao.module.famous.doctor.service.prescription;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.famous.doctor.controller.admin.prescription.vo.*;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.prescription.PrescriptionDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.famous.doctor.convert.prescription.PrescriptionConvert;
import cn.iocoder.yudao.module.famous.doctor.dal.mysql.prescription.PrescriptionMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.famous.doctor.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 药方统计 Service 实现类
 *
 * @author 档案
 */
@Service
@Validated
public class PrescriptionServiceImpl implements PrescriptionService {

    @Resource
    private PrescriptionMapper prescriptionMapper;

    @Override
    public Long createPrescription(PrescriptionCreateReqVO createReqVO) {
        // 插入
        PrescriptionDO prescription = PrescriptionConvert.INSTANCE.convert(createReqVO);
        prescriptionMapper.insert(prescription);
        // 返回
        return prescription.getId();
    }

    @Override
    public void updatePrescription(PrescriptionUpdateReqVO updateReqVO) {
        // 校验存在
        validatePrescriptionExists(updateReqVO.getId());
        // 更新
        PrescriptionDO updateObj = PrescriptionConvert.INSTANCE.convert(updateReqVO);
        prescriptionMapper.updateById(updateObj);
    }

    @Override
    public void deletePrescription(Long id) {
        // 校验存在
        validatePrescriptionExists(id);
        // 删除
        prescriptionMapper.deleteById(id);
    }

    private void validatePrescriptionExists(Long id) {
        if (prescriptionMapper.selectById(id) == null) {
            throw exception(PRESCRIPTION_NOT_EXISTS);
        }
    }

    @Override
    public PrescriptionDO getPrescription(Long id) {
        return prescriptionMapper.selectById(id);
    }

    @Override
    public List<PrescriptionDO> getPrescriptionList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return prescriptionMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<PrescriptionDO> getPrescriptionPage(PrescriptionPageReqVO pageReqVO) {
        return prescriptionMapper.selectPage(pageReqVO);
    }

    @Override
    public List<PrescriptionDO> getPrescriptionList(PrescriptionExportReqVO exportReqVO) {
        return prescriptionMapper.selectList(exportReqVO);
    }

}
